//
//  YXYWelcomeController.swift
//  SinaWeibo
//
//  Created by 袁向阳 on 16/6/26.
//  Copyright © 2016年 YXY.cn. All rights reserved.
//

import UIKit
import SDWebImage

class YXYWelcomeController: UIViewController {
    
    // 背景图
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // 头像
    private lazy var iconImageView: UIImageView = {
        let img = UIImageView(image: UIImage(named: "avatar_default_big"))
        img.layer.cornerRadius = 45
        img.layer.masksToBounds = true
        img.layer.borderColor = themeColor.CGColor
        img.layer.borderWidth = 3
        img.sd_setImageWithURL(NSURL(string: (YXYUserAccountTool.sharedUserAccountTool.userAccount?.avatar_large)!), placeholderImage: UIImage(named: "avatar_default_big"))
        return img
    }()
    
    // 姓名标签
    private var nameLabel: UILabel = {
        let lab = UILabel()
        lab.text = "\(YXYUserAccountTool.sharedUserAccountTool.userAccount?.screen_name ?? "")欢迎回来"
        lab.font = UIFont.systemFontOfSize(18)
        lab.textColor = UIColor.grayColor()
        // 对其方式
        lab.textAlignment = .Center
        // 透明度
        lab.alpha = 0
        return lab
    }()
    
    // 创建子控件
    func setUpUI () {
        
        view.addSubview(iconImageView)
        view.addSubview(nameLabel)
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.top.equalTo(view).offset(400)
            make.centerX.equalTo(view)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
        }
    }
    
    
    override func loadView() {
        super.loadView()
        
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        
        UIView.animateWithDuration(2, delay: 0.5, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    
                    self.nameLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        UIView.animateWithDuration(0.5, animations: { () -> Void in
                            
                            self.nameLabel.alpha = 0.9
                            
                            }, completion: { (_) -> Void in
                                
//                                UIApplication.sharedApplication().keyWindow?.rootViewController = YXYMainController()
                             
                                NSNotificationCenter.defaultCenter().postNotificationName(kChangeRootVC, object: "welcome")
                        })
                        
                })
                
        }
        
    }
    
}
